﻿//
//  SimplePRVid
//
//  Created by Andy Hatch on 2008-03-31.
//

//required PointRoll API installation

import mx.utils.Delegate;
import mx.events.EventDispatcher;
import PointRollAPI.media.PrVideo

class com.hatch.video.SimplePRVid
{	
	//on stage
	private var video_obj;
	private var _bufferMessage;
	private var _replayBtn;
	private var _dispatcher:EventDispatcher;
	
	//class properties
	private var _me;
	private var _prVideo:PrVideo;
	private var _videoFolder:String;
	private var _videoPath:String;
	private var _videoFiles:Array;
	private var _videoLengths:Array;
	private var _playing:Boolean;
	private var _view:MovieClip;
	private var _milestones:Array;
	
	function SimplePRVid(view:MovieClip, bufferMessage:MovieClip, vidObject, videoFolder:String, videoPath:String, vidFiles:Array, vidLengths:Array, replayBtn:MovieClip)
	{
		_me = this;
		video_obj = vidObject;
		_videoFolder = videoFolder;
		_videoPath = videoPath;
		_videoFiles = vidFiles;
		_videoLengths = vidLengths;
		_dispatcher = new EventDispatcher();
		_playing = false;
		_view = view;
		_bufferMessage = bufferMessage;
		_replayBtn = replayBtn;
		
		initialize();
	}
	
	
	private function initialize():Void
	{
		//trace("SimplePRVid :: initialize()");
		hideBufferMessage();
	}
	
	public function set milestones( a:Array ):Void
	{
		_milestones = a;
	}
	
	public function addEventListener(type:String, handler:Object):Void
	{
		_dispatcher.addEventListener(type, handler);
	}
	
	public function playVideo(videoNum:Number, volume:Number):Void
	{
		trace("SimplePRVid :: playVideo() : videoNum = " + videoNum);
		buildVideoPlayer(videoNum, volume);
		
		_playing = true;
		showBufferMessage();
	}
	
	public function killVideo():Void
	{
		_prVideo.killVideo();
	}
	
	public function fadeOutSound(vol:Number, time:Number):Void
	{
		_prVideo.fadeOutSound(vol, time);
	}
	
	public function fadeInSound(vol:Number, time:Number):Void
	{
		_prVideo.fadeInSound(vol, time);
	}
	
	public function mute():Void
	{
		_prVideo.fadeOutSound(0, .1);
	}
	
	public function unmute():Void
	{
		_prVideo.fadeInSound(100, .1);
	}
	
	public function replay():Void
	{
		_prVideo.replay();
	}
	
	private function buildVideoPlayer(videoNum:Number, volume:Number):Void
	{
		//PrVideo variables
		var videoFolder = _videoFolder;//"DomaniTests";//_videoFolder;//"Kelloggs";
		var progVideoPath:String = _videoPath + "/" + _videoFolder + "/" + _videoFiles[videoNum];
		var streamFile = _videoFiles[videoNum];
		var progVidLength = _videoLengths[videoNum];
		var pathToVideoObject = video_obj;
		var vidVolume = volume;
		var vidInstance = 1;
		var serverIndex = 0;
		
		
		_prVideo = new PrVideo(videoFolder, progVideoPath, streamFile, progVidLength, pathToVideoObject, vidVolume, vidInstance, _view.spVideoMC, serverIndex);
		
		_prVideo.optionalMilestones = _milestones;
		_prVideo.trackProgress = 0;
		_prVideo.trackEvents = false;
		//_prVideo.forceBuffer = 2;

		//Handlers
		_prVideo.onStart = Delegate.create(_me, onVideoStart);
		_prVideo.onPlay = Delegate.create(_me, onVideoPlay);
		_prVideo.onComplete = Delegate.create(_me, onVideoComplete);
		_prVideo.onOptionalMilestone = Delegate.create(_me, onVideoMilestone);
		_prVideo.onLowBandwidth = Delegate.create(_me, onLowBandwidth);
		_prVideo.onFail = Delegate.create(_me, onFail);
		_prVideo.onBufferEmpty = Delegate.create(_me, onBufferEmpty);
		_prVideo.onBufferFull = Delegate.create(_me, onBufferFull);
		
		_prVideo.startVideo(1);
	}
	
	private function onVideoMilestone(infoObj):Void
	{
		trace("\nSimplePRVid :: onVideoMilestone()" + infoObj.param + "\n");
		_dispatcher.dispatchEvent({type:"videoMilestone", target:_me, milestone:infoObj.param});
	}
	
	private function onLowBandwidth():Void
	{
		_prVideo.startVideo(2);
	}
	
	private function onFail():Void
	{
		//trace("SimplePRVid :: onFail()");
		_playing = false;
		hideBufferMessage();
		_dispatcher.dispatchEvent({type:"videoFail", target:_me});
	}
	
	private function onBufferEmpty():Void
	{
		//trace("SimplePRVid :: onBufferEmpty()");
		_dispatcher.dispatchEvent({type:"bufferOn", target:_me});
		showBufferMessage();
	}
	
	private function onBufferFull():Void
	{
		//trace("SimplePRVid :: onBufferFull()");
		_dispatcher.dispatchEvent({type:"bufferOff", target:_me});
		hideBufferMessage();
	}
	
	private function onVideoStart():Void
	{
		hideBufferMessage();
		_dispatcher.dispatchEvent({type:"videoStart", target:_me});
	}
	private function onVideoPlay():Void
	{
		hideBufferMessage();
	}
	private function onVideoComplete():Void
	{
		//trace("SimplePRVid :: onVideoComplete()")
		_playing = false;
		_dispatcher.dispatchEvent({type:"videoComplete", target:_me});
	}
	
	private function replayVideo():Void
	{
		_prVideo.killVideo();
		showBufferMessage();
		_prVideo.replay();
	}
	
	private function showBufferMessage():Void
	{
		//trace("SimplePRVid :: showBufferMessage()");
		if(_playing)
		{
			Delegate.create(_me, _bufferMessage.messageOn());
			//_bufferMessage._visible = true;
			//_bufferMessage._alpha = 100;
			
			_dispatcher.dispatchEvent({type:"bufferOn", target:_me});
		}	
	}
	
	private function hideBufferMessage():Void
	{
		//trace("SimplePRVid :: hideBufferMessage()");
		_bufferMessage.messageOff();
		_dispatcher.dispatchEvent({type:"bufferOff", target:_me});
	}
	
	public function showReplayBtn():Void
	{
		_replayBtn.gotoAndPlay('show');
	}
	
	public function hideReplayBtn():Void
	{
		//trace('SimplePRVid :: hideReplayBtn()');
		_replayBtn.gotoAndPlay('hide');
	}
}